<?php

namespace XWAM\Model;

/**
 * RuleModel类
 *
 * @since 1.0 <2015-10-19> SoChishun <14507247@qq.com> Added.
 */
class RuleModel extends AppbaseModel {

    protected $tableName = 'think_auth_rule';

    function select_tree($select_options = array(), $data_options = array()) {
        $select_options = array_merge(array('field' => 'id, title, pid, type, sort, status, create_time', 'order' => 'sort, id'), $select_options);
        return $this->base_get_tree_list($select_options, $data_options);
    }

    function select_json_tree($select_options = array(), $data_options = array()) {
        $select_options = array_merge(array('field' => 'id, title as text', 'order' => 'sort, id'), $select_options);
        return $this->base_get_tree_list($select_options, $data_options);
    }

    /**
     * 获取用户权限菜单
     * @return boolean
     */
    function get_menus() {
        if (user_check_login($login_data)) {
            $cache_key = 'menu_' . $login_data['id'];
            $data = S($cache_key);
            if ($data) {
                return $data;
            }
            // 2016-2-17 $group_ids = $this->table(C('AUTH_CONFIG.AUTH_GROUP_ACCESS'))->where(array('uid' => $login_data['id']))->getField('group_id', true);
            $group_ids = $this->table(C('AUTH_CONFIG.AUTH_USER'))->where(array('id' => $login_data['id']))->getField('role_ids');
            if (!$group_ids) {
                return false;
            }
            // 2016-2-17 $rule_ids = $this->table(C('AUTH_CONFIG.AUTH_GROUP'))->where(array('group_id' => array('in', $group_ids)))->getField('rules', true);
            // 2016-2-17 $rule_ids = implode(',', $rule_ids);
            $rule_ids = $this->table(C('AUTH_CONFIG.AUTH_GROUP'))->where(array('id' => array('in', $group_ids)))->getField('rules');

            if (!$rule_ids) {
                return false;
            }
            if (false !== strpos($rule_ids, '*')) {
                $rule_ids = '';
            }
            $data = $this->fn_get_menus(0, $rule_ids);
            S($cache_key, $data, 15);
            return $data;
        }
        return false;
    }

    /**
     * 加载菜单
     * @param int $pid
     * @param array $rule_ids 权限编号
     * @return mixed
     * @since 1.0 <2014-6-19> sutroon Added.
     * @since 2.0 <2015-10-19> SoChishun Added.
     */
    function fn_get_menus($pid = 0, $rule_ids = array()) {
        $where = array('pid' => $pid, 'type' => 'M');
        if ($rule_ids) {
            $where['id'] = array('in', $rule_ids);
        }
        $list = $this->cache(10)->where($where)->field('id, name, title, code, addon, url')->select();
        if ($list) {
            foreach ($list as &$row) {
                $row['children'] = $this->fn_get_menus($row['id'], $rule_ids);
            }
        }
        return $list;
    }

    /**
     * 获取侧边栏菜单
     * @param type $code
     * @return boolean
     */
    function get_aside_menus($code) {
        $data = $this->get_menus();
        if ($data) {
            foreach ($data as $r) {
                if ($code == $r['code']) {
                    return $r['children'];
                }
            }
        }
        return false;
    }

    function get_aside_menus_by_addon($addon = '') {
        if (!$addon) {
            $addon = ADDON_NAME;
        }
        $code = $this->where(array('addon' => $addon, 'pid' => 0))->getField('code');
        return $this->get_aside_menus($code);
    }

    /**
     * 获取历史访问菜单
     * @return boolean
     * @since 1.0 <2015-12-11> SoChishun Added.
     */
    function get_history_menus() {
        $name = 'menu_history';
        $sdata = cookie($name);
        if (!$sdata) {
            return false;
        }
        $adata = json_decode($sdata, true);
        if (!$adata) {
            return false;
        }
        $cur = $adata['cur'];
        $ids = $adata['ids'];
        $data = $this->get_menus();
        $out = array();
        foreach ($data as $r1) {
            if ($sub1 = $r1['children']) {
                foreach ($sub1 as $r2) {
                    if ($sub2 = $r2['children']) {
                        foreach ($sub2 as $r3) {
                            if (in_array($r3['id'], $ids)) {
                                $r3['active'] = $cur == $r3['id'];
                                $out[] = $r3;
                            }
                        }
                    }
                }
            }
        }
        return $out;
    }

}
